package com.version1_2.service.implement;

import com.util.property.Properties;
import com.version1_2.dao.SubscriDao1_2;
import com.version1_2.service.SubscriService1_2;
import net.sf.json.JSONArray;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by reeves on 2015/8/23.
 */

@Service
public class SubscriService12Impl implements SubscriService1_2 {

    /*创建log4j对象*/
    private static final Logger LOGGER = Logger.getLogger(SubscriService12Impl.class);

    @Autowired
    private SubscriDao1_2 subscriDao12;

    /**
     * 获取推荐界面信息
     *
     * @return
     */
    @Cacheable(value = {"subscriPageCacheVersion1_2"})
    @CacheEvict(value = "subscriPageCacheVersion1_2", condition = "#cleanCache==true", allEntries = true)
    public String getNewSubscriPage(boolean cleanCache) {
        Map<String, Object> recomMap = subscriDao12.getNewSubscriPageList();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        // 得到精选信息数组
        List<Map<String, Object>> chosenList = (List<Map<String, Object>>) recomMap.get("chosenList");

        // 得到整合信息数组
        List<Map<String, Object>> confirmList = (List<Map<String, Object>>) recomMap.get("confirmList");

        // 得到Break信息数组
        List<Map<String, Object>> breakList = (List<Map<String, Object>>) recomMap.get("breakList");

        // 得到专题一信息数组
        List<Map<String, Object>> subject1List = (List<Map<String, Object>>) recomMap.get("subject1List");

        // 得到专题一信息数组
        List<Map<String, Object>> subject2List = (List<Map<String, Object>>) recomMap.get("subject2List");

        // 得到专题一信息数组
        List<Map<String, Object>> subject3List = (List<Map<String, Object>>) recomMap.get("subject3List");

        // 得到专题一信息数组
        List<Map<String, Object>> subject4List = (List<Map<String, Object>>) recomMap.get("subject4List");

        // 得到专题一信息数组
        List<Map<String, Object>> subject5List = (List<Map<String, Object>>) recomMap.get("subject5List");

        // 得到专题一信息数组
        List<Map<String, Object>> subject6List = (List<Map<String, Object>>) recomMap.get("subject6List");

        // 得到大图信息数组
        List<Map<String, Object>> subscriTitleList = subscriDao12.getSubscriTitleList();
        for (int i = 0; i < subscriTitleList.size(); i++) {
            Map subscriMap = subscriTitleList.get(i);
            subscriMap.replace("imageSrc", (String) subscriMap.get("imageSrc") + Properties.ImageHighQuality);
            //subscriTitleList.add(i, subscriMap);
        }
        String subscriTitleListStr = JSONArray.fromObject(subscriTitleList).toString();


        /**
         * 1. 精选信息Json字符串
         * 格式：[{"pageId":,"imageSrc":"","firstTitle":"","recomPos":}...]
         */
        for (int i = 0; i < chosenList.size(); i++) {
            Map chosenMap = chosenList.get(i);
            chosenMap.replace("imageSrc", (String) chosenMap.get("imageSrc") + Properties.ImageQuality);
            //chosenList.add(i, chosenMap);
        }
        String chosenListStr = JSONArray.fromObject(chosenList).toString();

        /**
         * 2. 整合信息Json字符串
         * 格式：[{"pageId":,"imageSrc":"","firstTitle":"","recomPos":}]
         */
        for (int i = 0; i < confirmList.size(); i++) {
            Map confirmMap = confirmList.get(i);
            confirmMap.replace("imageSrc", (String) confirmMap.get("imageSrc") + Properties.ImageQuality);
            //confirmList.add(i, confirmMap);
        }
        String confirmListStr = JSONArray.fromObject(confirmList).toString();


        boolean break2IsArticle = false; // 第二个break默认不是文章
        boolean break4IsArticle = false; // 第四个break默认不是文章

        for (int i = 0; i < breakList.size(); i++) {
            Map breakMap = breakList.get(i);
            int recomPos = (int) breakMap.get("recomPos");
            if (recomPos == 11) {
                breakMap.put("breakNum", 1);
                breakMap.replace("imageSrc", (String) breakMap.get("imageSrc") + Properties.ImageHighQuality);
            }
            if (recomPos == 13) {
                break2IsArticle = true;
                breakMap.put("breakNum", 2);
                breakMap.replace("imageSrc", (String) breakMap.get("imageSrc") + Properties.ImageHighQuality);
            }
            if (recomPos == 15) {
                breakMap.put("breakNum", 3);
                breakMap.replace("imageSrc", (String) breakMap.get("imageSrc") + Properties.ImageHighQuality);
            }
            if (recomPos == 17) {
                break4IsArticle = true;
                breakMap.put("breakNum", 4);
                breakMap.replace("imageSrc", (String) breakMap.get("imageSrc") + Properties.ImageHighQuality);
            }
            if (recomPos == 19) {
                breakMap.put("breakNum", 5);
                breakMap.replace("imageSrc", (String) breakMap.get("imageSrc") + Properties.ImageHighQuality);
            }
            breakMap.remove("recomPos");

            //breakList.add(i, breakMap);
        }

        String picPostfex = simpleDateFormat.format(new Date()).concat(".jpg");

        // 如果Break2不是文章
        if (!break2IsArticle) {
            Map<String, Object> break2Map = new HashMap<>();
            break2Map.put("breakNum", 2);
            break2Map.put("imageSrc", Properties.Break2Url + picPostfex);
            breakList.add(break2Map);
        }

        // 如果Break4不是文章
        if (!break4IsArticle) {
            Map<String, Object> break4Map = new HashMap<>();
            break4Map.put("breakNum", 4);
            break4Map.put("imageSrc", Properties.Break4Url + picPostfex);
            breakList.add(break4Map);
        }

        /**
         * 3. Break Json字符串
         * 格式：[{"breakNum":,"pageId":,"imageSrc":"","firstTitle":""},{"breakNum":,"imageSrc":""}...]
         */
        String breakListStr = JSONArray.fromObject(breakList).toString();

        List<Map<String, Object>> subjectList = new ArrayList<>();

        Map subject1Map = new HashMap<>();
        subject1Map.put("subNum", 1);
        subject1Map.put("streamer", Properties.Subject1Url + picPostfex);
        for (int i = 0; i < subject1List.size(); i++) {
            Map subject = subject1List.get(i);
            subject.replace("imageSrc", (String) subject.get("imageSrc") + Properties.ImageQuality);
            //subject1List.add(i, subject);
        }
        subject1Map.put("pageList", subject1List);
        subjectList.add(subject1Map);


        Map subject2Map = new HashMap<>();
        subject2Map.put("subNum", 2);
        subject2Map.put("streamer", Properties.Subject2Url + picPostfex);
        for (int i = 0; i < subject2List.size(); i++) {
            Map subject = subject2List.get(i);
            subject.replace("imageSrc", (String) subject.get("imageSrc") + Properties.ImageQuality);
            //subject2List.add(i, subject);
        }
        subject2Map.put("pageList", subject2List);
        subjectList.add(subject2Map);


        Map subject3Map = new HashMap<>();
        subject3Map.put("subNum", 3);
        subject3Map.put("streamer", Properties.Subject3Url + picPostfex);
        for (int i = 0; i < subject3List.size(); i++) {
            Map subject = subject3List.get(i);
            subject.replace("imageSrc", (String) subject.get("imageSrc") + Properties.ImageQuality);
            //subject3List.add(i, subject);
        }
        subject3Map.put("pageList", subject3List);
        subjectList.add(subject3Map);


        Map subject4Map = new HashMap<>();
        subject4Map.put("subNum", 4);
        subject4Map.put("streamer", Properties.Subject4Url + picPostfex);
        for (int i = 0; i < subject4List.size(); i++) {
            Map subject = subject4List.get(i);
            subject.replace("imageSrc", (String) subject.get("imageSrc") + Properties.ImageQuality);
            //subject4List.add(i, subject);
        }
        subject4Map.put("pageList", subject4List);
        subjectList.add(subject4Map);


        Map subject5Map = new HashMap<>();
        subject5Map.put("subNum", 5);
        subject5Map.put("streamer", Properties.Subject5Url + picPostfex);
        for (int i = 0; i < subject5List.size(); i++) {
            Map subject = subject5List.get(i);
            subject.replace("imageSrc", (String) subject.get("imageSrc") + Properties.ImageQuality);
            //subject5List.add(i, subject);
        }
        subject5Map.put("pageList", subject5List);
        subjectList.add(subject5Map);


        Map subject6Map = new HashMap<>();
        subject6Map.put("subNum", 6);
        subject6Map.put("streamer", Properties.Subject6Url + picPostfex);
        for (int i = 0; i < subject6List.size(); i++) {
            Map subject = subject6List.get(i);
            subject.replace("imageSrc", (String) subject.get("imageSrc") + Properties.ImageQuality);
        }
        subject6Map.put("pageList", subject6List);
        subjectList.add(subject6Map);

        /**
         * 4. 专题文章Json字符串
         * 格式：[{"subNum":,"streamer":"","pageList":[{"pageId":,"imageSrc":"","firstTitle":"","readNumber":,"thirdTypeName":""}...]}...]
         */
        String subjectListStr = JSONArray.fromObject(subjectList).toString();


        String recomStr = "{\"chosenList\":";
        recomStr = recomStr.concat(chosenListStr + ",");
        recomStr = recomStr.concat("\"chosen-confirm-break\":\"" + Properties.Chosenconfirm_BreakUrl + "\",");
        recomStr = recomStr.concat("\"confirm-subject_break\":\"" + Properties.Confirmsubject_BreakUrl + "\",");
        recomStr = recomStr.concat("\"confirmList\":" + confirmListStr + ",");
        recomStr = recomStr.concat("\"breakList\":" + breakListStr + ",");
        recomStr = recomStr.concat("\"subjectList\":" + subjectListStr + ",");
        recomStr = recomStr.concat("\"title\":" + subscriTitleListStr);
        recomStr = recomStr.concat("}");
        return recomStr;
    }
}
